package com.anjian.action;

import java.util.logging.Level;

import com.anjian.taobao.TaobaoProxy;
import com.taobao.api.ApiException;
import com.taobao.api.domain.Item;
import com.taobao.api.response.ItemUpdateResponse;

public class DetectItemAction extends ActionBase {
	
	private int index;
	
	public String execute()
	{
		Item item = getItem(index);
		String sessionId = getSessionId();
		detectItem(item.getNumIid(), sessionId);
		return SUCCESS;
	}

	private void detectItem(long numIid, String sessionId)
	{
//		if (!notRepeatCids.containsKey(item.getCid()))
//		{
			try {
				ItemUpdateResponse rsp = TaobaoProxy.updateItem(sessionId, numIid);
				if (!rsp.isSuccess())
				{
					_log.info("code: " + rsp.getErrorCode() + " msg: " + rsp.getMsg() + " subCode: " + rsp.getSubCode() + " subMsg: " + rsp.getSubMsg());
					String subCode = rsp.getSubCode();
//					if (isRepeatError(subCode))
//					{
//						_log.info("ignore cid: " + item.getCid());
//						notRepeatCids.put(item.getCid(), item.getCid());
//					}
//					if (subCode != null && !isSystemError(subCode))
//					{
//						rsp.setItem(item); 
//						errorItems.add(rsp);
//					}
//					if (errorItems.size() >= MAX_ERROR)
//					{
//						return;
//					}
				}
				else
				{
					Item item = rsp.getItem();
					_log.info("checked: " + item.getNumIid() + " - " + item.getTitle());
				}
			} catch (ApiException e) {
				_log.log(Level.SEVERE, "", e);
			}
			catch (Exception e) {
				_log.log(Level.SEVERE, "", e);
			}
//		}
		increaceProgress();
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public int getIndex() {
		return index;
	}
}
